import React, { useState, useEffect, useContext } from 'react';
import { Row, Col, Card, Form, Input, Icon, Button, Table, Tabs, Select, Spin, Descriptions, Modal} from 'antd';
import {connect} from 'dva';
import dynamic from 'umi/dynamic';
import LoadingComponent from '@/components/PageLoading/index';
import ProblemApply from '../common/problem/apply/ProblemApply';
import FunctionConfApply from '../common/functionConf/apply/FunctionConfApply';
import IctApply from '../common/ict/apply/IctApply';
import UpgradeApply from '../common/upgrade/apply/UpgradeApply';
import TroubleApply from '../common/trouble/apply/TroubleApply';
import SettlementApply from '../common/settlement/apply/SettlementApply';

const {info} = Modal;

const namespace = 'afterSale';
function AfterSaleAdd(props) {
  const [callBackMethod, setCallBackMethod] = useState({});
  const [tempFormRef,setTempFormRef] = useState(null);
  const [tempFileData,setTempFileData] = useState([]);
  // 初始化
  useEffect(() => {
  }, [props.selectRecord]);

  const saveDisplay = params => {
    const {dispatch} = props;
    dispatch({
      type: `${namespace}/saveDisplay`,
      payload: params,
    });
  };

  const backMainPage = isRealod => {
    saveDisplay("block");
    saveSelectRecord({});
    if (callBackMethod.clearData != null) {
      let params = {};
      params.tempFormRef = tempFormRef;
      callBackMethod.clearData(params);
    }
  };

  const handleSubmit = async(submitType) => {
    try {
      let params = {};
      params.submitType = submitType;
      params.tempFormRef = tempFormRef;
      params.fileData = tempFileData;
      const modifyResult = await callBackMethod.submitData(params);
      if (!modifyResult) {
        return;
      }else{
        info({title: "提交成功", content: ("数据提交成功")});
        backMainPage();
      }
    } catch (error) {
      console.log(error)
      info({ title: '请确认', content: '提交修改失败' });
      return;
    }
  };

  const getAfterSaleAdd = () => {
    switch (props.supportType) {
      case '1000':
        return (<ProblemApply
                setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                selectRecord = {props.selectRecord}
              />);
      case '1100':
        return (<FunctionConfApply
                setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                selectRecord = {props.selectRecord}
              />);
      case '1200':
        return (<TroubleApply
                  setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                  setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                  setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                  selectRecord = {props.selectRecord}
              />);
      case '1300':
        return (<UpgradeApply
                setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                selectRecord = {props.selectRecord}
              />);
      case '1400':
        return (<IctApply
                setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                selectRecord = {props.selectRecord}
              />);
      case '1500':
        return (<SettlementApply
                setCallBackMethod={callBackMethod => setCallBackMethod(callBackMethod)}
                setTempFormRef = {tempFormRef => setTempFormRef(tempFormRef)}
                setTempFileData = {tempFileData => setTempFileData(tempFileData)}
                selectRecord = {props.selectRecord}
              />);
      default:
        return <sapn>{props.supportType}</sapn>
    }
  };

  // 通过dva存当前需要传递的数据
  const saveSelectRecord = params => {
    const {dispatch} = props;
    dispatch({
      type: `${namespace}/saveSelectRecord`,
      payload: params,
    });
  };

  return(
    <div>
      <Card
        title="售中售后申请单"
        className="cute"
        extra={
          <Button className="margin-left" type="primary" onClick={() => {
            backMainPage()
          }}>
            返回
          </Button>
        }
        >
        {getAfterSaleAdd()}

        {
          <Card
            bordered={false}
          >
          <div style={{ textAlign: 'center' }}>
              <Button
                type="primary"
                className="margin-right"
                onClick={() => {handleSubmit("save")}}
              >
                保存
              </Button>
              <Button
                type="primary"
                className="margin-right"
                onClick={() => {handleSubmit("submit")}}
              >
                提交
              </Button>
              <Button
                type="default"
                onClick={() => {
                  backMainPage();
                }}
              >
                返回
              </Button>
            </div>
            </Card>
          }
      </Card>
    </div>
  )
}
export default connect(({afterSale, setting}) => ({
  display: afterSale.display,
  selectRecord: afterSale.selectRecord,
  supportType:afterSale.supportType,
  size: setting.size,
}))(Form.create()(AfterSaleAdd));
